﻿#include "timer_event.h"
#include "common.h"
#include <system/Thread.h>
#include <set>
//#include "logic/global_variables.h"
#include "logic/logic_utility.hpp"

#define HOT_WATER_NUM 3000

typedef struct {
    ETaskId task_id;          // 任务ID
    ETaskPriority task_period; // 任务优先级
    bool is_running;          // 任务是否正在运行
    int load_bit;             // 负载位掩码，表示当前任务关联的负载状态
} S_TaskInfo;

namespace water {

//事件总时间数组
static int _events_time[] = {
    ALL_BASH4_TIME_POINT,
    ICE_ADD_WATER_TIME_POINT,
    MAKE_WATER_TIMEOUT_TIME_POINT,
    FILTER_BRASH_TIME_POINT,
    DRAIN_WATER_LONG_TIME_POINT,

    DRAIN_WATER_SHORT_TIME_POINT,
    ZERO_STATE_WATER_A4_TIME_POINT,
    ZERO_STATE_WATER_B4_TIME_POINT,
    WATER_CHECK_PRESS_TEST_TIME_POINT,
	LED_OFF_TIME_POINT,

	PREVENT_DRY_BURNING_TIME_POINT,
	HOT_WATER_DELAY_TIME_POINT,
	TIME_POINT_60,		//停制冷
	TIME_POINT_300,	//制冰补水
	TIME_POINT_12,		//制冰泵启动

	TIME_POINT_35,			//融冰操作
	TIME_POINT_1,				//冰满
	TIME_POINT_4,			//冰块防沾
};

static S_TaskInfo _task_info[] = {
	{E_TASK_ID_ALL_BRASH, E_TASK_PRIORITY_0, false, 0},
	{E_TASK_ID_ICE_ADD_WATER, E_TASK_PRIORITY_0, false, 0},

	{E_TASK_ID_MAKE_WATER_TIMEOUT, E_TASK_PRIORITY_0, false, 0},
	{E_TASK_ID_FILTER_BRASH, E_TASK_PRIORITY_0, false, 0},
	{E_TASK_ID_DRAIN_WATER, E_TASK_PRIORITY_0, false, 0},
	{E_TASK_ID_ZERO_STATE_WATER, E_TASK_PRIORITY_0, false, 0},

	{E_TASK_ID_WATER_CHECK_PRESS_TEST, E_TASK_PRIORITY_0, false, 0},
	{E_TASK_ID_WATER_OPERATE, E_TASK_PRIORITY_0, false, 0},
	{E_TASK_ID_STOP_REFRIGERATION, E_TASK_PRIORITY_0, false, 0},
	{E_TASK_ID_MAKE_ICE, E_TASK_PRIORITY_0, false, 0},
	{E_TASK_ID_MOTOR_ICE, E_TASK_PRIORITY_0, false, 0},
};

static std::set<timer_cb> _s_cb_set;
static Mutex _s_mutex;

static void _notify_timer_event(int id, int run_time, int total_time, void *user_data)
{
    // Mutex::Autolock _l(_s_mutex);
    for(auto cb : _s_cb_set) {
        cb(id, run_time, total_time, user_data);
    }
}

static int get_event_bit(std::vector<E_DEV_LOAD_TYPE> load_types) {
	int event_bit = 0;
	    // 遍历负载类型列表，将每个负载类型的值转换为二进制位掩码，并进行按位或操作
	    for (auto load_type : load_types) {
	        event_bit |= (1 << (int)load_type);  // 将负载类型值 左移load_type位
	    }
	    return event_bit;  // 返回最终的位掩码
}

bool is_higher_priority(ETaskId task_id) {
	for(size_t i = 0; i < TAB_SIZE(_task_info); i++) {
		if(task_id != _task_info[i].task_id) {	//不同负载之间进行对比
			if(_task_info[i].is_running) {
				if(_task_info[i].task_period < _task_info[task_id].task_period) {
					return true;
				}
			}
		}
	}
	return false;
}

static void delete_same_load_type(std::vector<E_DEV_LOAD_TYPE>& load_types, int load_bit) {
	for(size_t i = 0; i < load_types.size(); i++) {
//		LOGD("需要关 %d 位",(int)load_types[i]);
		//左移n位
		if(load_bit & (1 << (int)load_types[i])) {
//			LOGD("load_types %d 位保留",(int)load_types[i]);
			load_types.erase(load_types.begin() + i);
			i--;
		}
	}
}

// 实现设置任务负载的函数
bool set_task_load(ETaskId task_id, bool is_on, std::vector<E_DEV_LOAD_TYPE> load_types, bool is_stop) {
    // 使用互斥锁确保线程安全
    Mutex::Autolock _l(protocol::getAckLock());

    if (is_on) {  // 如果是开启任务负载
        // 检查当前任务是否有更高的优先级冲突
//        if (is_higher_priority(task_id)) {
//            return false;  // 如果有更高优先级任务，则不允许启动
//        } else
        {
            // 获取当前任务的负载位掩码
            _task_info[task_id].load_bit |= get_event_bit(load_types);
//            if(task_id == 9){
//            	LOGD("制冰开启load_bit: %d",_task_info[task_id].load_bit)
//            }
            // 遍历负载类型列表，逐一开启对应的负载
            for (auto load_type : load_types) {
                protocol::setLoadSwitch(load_type, true);  // 开启负载
            }
        }
        // 更新任务状态为运行中
        _task_info[task_id].is_running = is_on;
    } else {  // 如果是关闭任务负载
        std::vector<E_DEV_LOAD_TYPE> off_load_types = load_types;  // 复制需要关闭的负载列表
        // 遍历所有任务，检查其他任务是否也在使用相同的负载
        for (size_t i = 0; i < TAB_SIZE(_task_info); i++) {
            if (_task_info[i].is_running) {  // 如果某个任务正在运行
                if (task_id != _task_info[i].task_id) {  // 不同任务之间才考虑保留负载
//                	LOGD("任务正在运行:%d",_task_info[i].task_id)
                    // 如果其他任务的优先级高于或等于当前任务，则保留负载
                    if (_task_info[i].task_period <= _task_info[task_id].task_period) {
//                    	LOGD("优先级一致，load_bit: %d",_task_info[i].load_bit)
                        delete_same_load_type(off_load_types, _task_info[i].load_bit);  // 删除重复的负载类型
                    }
                }
            }
        }
        // 关闭不需要的负载
        for (auto load_type : off_load_types) {
            _task_info[task_id].load_bit &= ~(1 << (int)load_type);  // 清除对应的负载位
            protocol::setLoadSwitch(load_type, false);  // 关闭负载
        }
        // 如果需要完全停止任务
        if (is_stop) {
            _task_info[task_id].load_bit = 0;  // 清空负载位掩码
            _task_info[task_id].is_running = is_on;  // 更新任务状态为停止
        }
    }
    return true;  // 返回成功
}

bool is_task_running(ETaskId task_id) {
	if(task_id >= 0 || task_id < TAB_SIZE(_task_info)){
		return _task_info[task_id].is_running;
	}
	LOGD("out of [_task_info] range\n");
	return false;
}

static void on_timer_cb(int id, int run_time, int total_time, void *user_data) {
	LOGD("[timer event] time id %d, run time %d, total time %d\n", id, run_time, total_time);
		// 调用通知函数，触发UI的回调
	    _notify_timer_event(id, run_time, total_time, user_data);

	    // 根据定时器事件 ID 和运行时间点，执行不同的逻辑
	    if (id == E_TIMER_EVENT_ALL_BRASH) {
	        // 整机冲洗
	        if (run_time == START_TIME_POINT) {
	            // 启动空气泵、进水阀和废水阀
	            set_task_load(E_TASK_ID_ALL_BRASH, true, {E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_WASTE_WATER});
	        } else if (run_time == ALL_BASH1_TIME_POINT) {
	            // 关闭废水阀，启动常温阀
	            set_task_load(E_TASK_ID_ALL_BRASH, false, {E_DEV_LOAD_TYPE_WASTE_WATER}, false);
	            set_task_load(E_TASK_ID_ALL_BRASH, true, {E_DEV_LOAD_TYPE_COMMON_TEMP});
	        } else if (run_time == ALL_BASH2_TIME_POINT) {
	            // 关闭常温阀，启动冷水阀
	            set_task_load(E_TASK_ID_ALL_BRASH, false, {E_DEV_LOAD_TYPE_COMMON_TEMP}, false);
	            set_task_load(E_TASK_ID_ALL_BRASH, true, {E_DEV_LOAD_TYPE_COLD_WATER});
	        } else if (run_time == ALL_BASH3_TIME_POINT) {
	            // 关闭冷水阀和冷却设备，启动 UV 灯，并开始出水
	            set_task_load(E_TASK_ID_ALL_BRASH, false, {E_DEV_LOAD_TYPE_COLD_WATER, E_DEV_LOAD_TYPE_COOLING}, false);
	            set_task_load(E_TASK_ID_ALL_BRASH, true, {E_DEV_LOAD_TYPE_UV_LIGHT});
	            protocol::startOutputWater(25, HOT_WATER_NUM);
	        } else if (run_time == ALL_BASH3_TIME_POINT + 1) {
	            // 再次启动出水
	            protocol::startOutputWater(25, HOT_WATER_NUM, true);
	        } else if (run_time == ALL_BASH4_TIME_POINT) {
	            // 关闭所有设备负载，并停止出水
	            set_task_load(E_TASK_ID_ALL_BRASH, false, {
	                E_DEV_LOAD_TYPE_UV_LIGHT, E_DEV_LOAD_TYPE_COMMON_TEMP,
	                E_DEV_LOAD_TYPE_COLD_WATER, E_DEV_LOAD_TYPE_COOLING,
	                E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WATER_IN,
	                E_DEV_LOAD_TYPE_WASTE_WATER});
	            protocol::stopOutputWater();
	        }
	    }
	    else if (id == E_TIMER_EVENT_ICE_ADD_WATER) {
	        // 冰水箱补水
	        if (run_time == START_TIME_POINT) {
	            // 整机进水电磁阀’+‘增压泵’+“冰水箱进水阀”
	            set_task_load(E_TASK_ID_ICE_ADD_WATER, true, {E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_AIR_PUMP,E_DEV_LOAD_TYPE_ICETANK_IN} );
	        } else if (run_time == ICE_ADD_WATER_TIME_POINT) {
	            set_task_load(E_TASK_ID_ICE_ADD_WATER, false,{E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_AIR_PUMP,E_DEV_LOAD_TYPE_ICETANK_IN}  );
	        }
	    }

	    else if (id == E_TIMER_EVENT_STOP_REFRIGERATION) {
	        // 停止制冷事件
	        if (run_time == START_TIME_POINT) {
	            // 关闭制冷 220V 电源，关闭循环泵
	            set_task_load(E_TASK_ID_STOP_REFRIGERATION, false, {E_DEV_LOAD_TYPE_COMPRESSOR, E_DEV_LOAD_TYPE_CIRCULATION_PUMP},false);
	        } else if (run_time ==TIME_POINT_60) {
	            // 关闭进气风扇和冷凝器风扇
	            set_task_load(E_TASK_ID_STOP_REFRIGERATION, false, {E_DEV_LOAD_TYPE_INTAKE_FAN, E_DEV_LOAD_TYPE_CONDENSER_FAN});
	        }
	    }

	    else if (id == E_TIMER_EVENT_ICE_ADD_BLOCK) {
	        //制冰块箱补水
	        if (run_time == START_TIME_POINT) {
	            // 打开“制冰水进水阀”，
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, { E_DEV_LOAD_TYPE_MAKEICE_IN, } );

	        }else if(run_time == TIME_POINT_300){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_MAKEICE_IN, } , false );
	        }
	    }
	    else if (id == E_TIMER_EVENT_MAKE_ICEPUMP) {
	        //制冰泵
	        if (run_time == START_TIME_POINT) {
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP, } );
	        }else if (run_time ==TIME_POINT_4){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP }, false );

	        }else if (run_time ==TIME_POINT_6){
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP, } );
	        }else if (run_time ==TIME_POINT_10){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP }, false );

	        }else if (run_time ==TIME_POINT_12){
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP, } );
	        }
	    }
	    else if (id == E_TIMER_EVENT_INTER_CLEAN) {
	        //制冰泵 内部清洗
	        if (run_time == START_TIME_POINT) {
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP, } );
	        }else if (run_time ==TIME_POINT_4){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP }, false );

	        }else if (run_time ==TIME_POINT_6){
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP, } );
	        }else if (run_time ==TIME_POINT_10){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP }, false );

	        }else if (run_time ==TIME_POINT_12){
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP, } );
	        }else if(run_time == total_time){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP }, false );
	        	getProtocolParam().control_aa_a1.CmdDrainBoxPump = true;		//打开“接水盒排水泵”负载
	        }
	    }
	    else if (id == E_TIMER_EVENT_MAKE_ALLICE) {
	        //所有冰块
	        if (run_time == START_TIME_POINT) {
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, {	E_DEV_LOAD_TYPE_COMPRESSOR, E_DEV_LOAD_TYPE_INTAKE_FAN, E_DEV_LOAD_TYPE_CONDENSER_FAN,});
	        }
	    	else if (run_time == total_time - 60) {
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, {E_DEV_LOAD_TYPE_HEAT_FILM, });
	        }else if(run_time == total_time){
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, {E_DEV_LOAD_TYPE_ICE_OUT, E_DEV_LOAD_TYPE_PUSH_FORWARD});
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, {E_DEV_LOAD_TYPE_CONDENSER_FAN, E_DEV_LOAD_TYPE_ICEMAKE_PUMP, }, false);
	        	if(!Cur_a2.IceMakingWaterLevel){
	        		start_timer_event(E_TIMER_EVENT_ICE_ADD_BLOCK);
	        	}
	        }
	    }

	    else if (id == E_TIMER_EVENT_ROD_MOTOR) {
	        //推杆电机控制
	        if (run_time == START_TIME_POINT) {
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, {E_DEV_LOAD_TYPE_COMPRESSOR, E_DEV_LOAD_TYPE_INTAKE_FAN});
	            // 关闭“推杆电机正”，
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_PUSH_FORWARD }, false );
	        }else if(run_time == TIME_POINT_25){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_HEAT_FILM }, false );
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, {E_DEV_LOAD_TYPE_PUSH_REVERSE, });

	        }else if(run_time == TIME_POINT_35){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_PUSH_REVERSE }, false );
	        	Global::Flags::ice_sheded = true;
	        }
	    }
	    else if (id == E_TIMER_EVENT_ICE_STICK) {
	        // 冰块防沾
	        if (run_time == START_TIME_POINT) {
	        	set_task_load(E_TASK_ID_MOTOR_ICE, true, {E_DEV_LOAD_TYPE_MOTOR_FORWARD, });
	        }else if(run_time == TIME_POINT_2){
	        	set_task_load(E_TASK_ID_MOTOR_ICE, false, {E_DEV_LOAD_TYPE_MOTOR_FORWARD, }, false);
	        	set_task_load(E_TASK_ID_MOTOR_ICE, true, {E_DEV_LOAD_TYPE_MOTOR_REVERSE, });
	        } else if(run_time == TIME_POINT_4){
	        	set_task_load(E_TASK_ID_MOTOR_ICE, false, {E_DEV_LOAD_TYPE_MOTOR_REVERSE, });
			}
	    }
	    else if (id == E_TIMER_EVENT_OUT_ICE) {
	        // 出冰
	        if (run_time == START_TIME_POINT) {
	        	set_task_load(E_TASK_ID_MOTOR_ICE, true, {E_DEV_LOAD_TYPE_MOTOR_FORWARD, });
	        }else if(run_time == total_time - 2){
	        	set_task_load(E_TASK_ID_MOTOR_ICE, false, {E_DEV_LOAD_TYPE_MOTOR_FORWARD, }, false);
	        	set_task_load(E_TASK_ID_MOTOR_ICE, true, {E_DEV_LOAD_TYPE_MOTOR_REVERSE, });
	        }else if(run_time == total_time){
	        	set_task_load(E_TASK_ID_MOTOR_ICE, false, {E_DEV_LOAD_TYPE_MOTOR_REVERSE, });
	        }
	    }else if (id == E_TIMER_EVENT_STOP_ICE) {
	        // 停冰
	        if (run_time == START_TIME_POINT) {
	        	set_task_load(E_TASK_ID_MOTOR_ICE, false, {E_DEV_LOAD_TYPE_MOTOR_FORWARD, }, false);
	        	set_task_load(E_TASK_ID_MOTOR_ICE, true, {E_DEV_LOAD_TYPE_MOTOR_REVERSE, });
	        }else if(run_time == TIME_POINT_2){
	        	set_task_load(E_TASK_ID_MOTOR_ICE, false, {E_DEV_LOAD_TYPE_MOTOR_REVERSE, });
	        }
	    }
	    else if (id == E_TIMER_EVENT_FORCE_OUT) {
	        // 强制脱冰
	        if (run_time == START_TIME_POINT) {
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, {E_DEV_LOAD_TYPE_PUSH_FORWARD, }, false);
	        }else if(run_time == TIME_POINT_30){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, {E_DEV_LOAD_TYPE_HEAT_FILM, }, false);
	        	set_task_load(E_TASK_ID_MAKE_ICE, true, {E_DEV_LOAD_TYPE_PUSH_REVERSE, });
	        }else if(run_time == TIME_POINT_40){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, {E_DEV_LOAD_TYPE_PUSH_REVERSE, }, false);
	        }else if(run_time == total_time){
	        	set_task_load(E_TASK_ID_MAKE_ICE, false, {E_DEV_LOAD_TYPE_COMPRESSOR, E_DEV_LOAD_TYPE_ICE_OUT});
	        	forceDefrost(false);
	        }
	    }
	    else if (id == E_TIMER_EVENT_MAKE_WATER_TIMEOUT) {
	        // 制水超时事件
	        if (run_time == START_TIME_POINT) {
	            // 启动增压泵和进水阀
	            set_task_load(E_TASK_ID_MAKE_WATER_TIMEOUT, true, {E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WATER_IN});
	        } else if (run_time == MAKE_WATER_TIMEOUT_TIME_POINT) {
	            // 关闭增压泵和进水阀
	            set_task_load(E_TASK_ID_MAKE_WATER_TIMEOUT, false, {E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WATER_IN}, false);
	        }
	    }
	    else if (id == E_TIMER_EVENT_FILTER_BRASH) {
	        // 滤芯刷洗事件
	        if (run_time == START_TIME_POINT) {
	            // 启动空气泵、进水阀和废水阀
	            set_task_load(E_TASK_ID_FILTER_BRASH, true, {E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_WASTE_WATER});
	        } else if (run_time == FILTER_BRASH_TIME_POINT) {
	            // 关闭空气泵、进水阀和废水阀
	            set_task_load(E_TASK_ID_FILTER_BRASH, false, {E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_WASTE_WATER});
	        }
	    }
	    else if (id == E_TIMER_EVENT_DRAIN_WATER_LONG || id == E_TIMER_EVENT_DRAIN_WATER_SHORT) {
	        // 长/短排水事件
	        if (run_time == START_TIME_POINT) {
	            // 启动所有相关设备负载，并开始出水
	            set_task_load(E_TASK_ID_DRAIN_WATER, true, {
	                E_DEV_LOAD_TYPE_WASTE_WATER, E_DEV_LOAD_TYPE_COMMON_TEMP,
	                E_DEV_LOAD_TYPE_COLD_WATER, E_DEV_LOAD_TYPE_AIR_PUMP,
	                E_DEV_LOAD_TYPE_COOLING, E_DEV_LOAD_TYPE_UV_LIGHT,
	                E_DEV_LOAD_TYPE_WATER_IN});
	            protocol::startOutputWater(25, HOT_WATER_NUM);
	        } else if (run_time == START_TIME_POINT + 1) {
	            // 再次启动出水
	            protocol::startOutputWater(25, HOT_WATER_NUM, true);
	        } else if ((id == E_TIMER_EVENT_DRAIN_WATER_LONG && run_time == DRAIN_WATER_LONG_TIME_POINT) ||
	                   (id == E_TIMER_EVENT_DRAIN_WATER_SHORT && run_time == DRAIN_WATER_SHORT_TIME_POINT)) {
	            // 关闭所有相关设备负载，并停止出水
	            set_task_load(E_TASK_ID_DRAIN_WATER, false, {
	                E_DEV_LOAD_TYPE_WASTE_WATER, E_DEV_LOAD_TYPE_COMMON_TEMP,
	                E_DEV_LOAD_TYPE_COLD_WATER, E_DEV_LOAD_TYPE_AIR_PUMP,
	                E_DEV_LOAD_TYPE_COOLING, E_DEV_LOAD_TYPE_UV_LIGHT,
	                E_DEV_LOAD_TYPE_WATER_IN});
	            protocol::stopOutputWater();
	        }
	    }
	    else if (id == E_TIMER_EVENT_ZERO_STATE_WATER_A || id == E_TIMER_EVENT_ZERO_STATE_WATER_B) {
	        // 零陈水 A/B 事件
	        if (run_time == ZERO_STATE_WATER_A1_TIME_POINT || run_time == ZERO_STATE_WATER_B1_TIME_POINT) {
	            // 启动进水阀、空气泵和 UV 灯
	            set_task_load(E_TASK_ID_ZERO_STATE_WATER, true, {E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_UV_LIGHT});
	        } else if (run_time == ZERO_STATE_WATER_A2_TIME_POINT || run_time == ZERO_STATE_WATER_B2_TIME_POINT) {
	            // 关闭 UV 灯，启动废水阀
	            set_task_load(E_TASK_ID_ZERO_STATE_WATER, false, {E_DEV_LOAD_TYPE_UV_LIGHT}, false);
	            set_task_load(E_TASK_ID_ZERO_STATE_WATER, true, {E_DEV_LOAD_TYPE_WASTE_WATER});
	        } else if (run_time == ZERO_STATE_WATER_A3_TIME_POINT || run_time == ZERO_STATE_WATER_B3_TIME_POINT) {
	            // 关闭进水阀和空气泵
	            set_task_load(E_TASK_ID_ZERO_STATE_WATER, false, {E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_AIR_PUMP}, false);
	        } else if (run_time == ZERO_STATE_WATER_A4_TIME_POINT || run_time == ZERO_STATE_WATER_B4_TIME_POINT) {
	            // 关闭所有设备负载
	            set_task_load(E_TASK_ID_ZERO_STATE_WATER, false, {
	                E_DEV_LOAD_TYPE_UV_LIGHT, E_DEV_LOAD_TYPE_WATER_IN,
	                E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WASTE_WATER});
	        }
	    }

	    else if (id == E_TIMER_EVENT_WATER_CHECK_PRESS_TEST) {
	        // 水压检测测试事件
	        if (run_time == START_TIME_POINT) {
	            // 启动进水阀和 UV 灯
	            set_task_load(E_TASK_ID_WATER_CHECK_PRESS_TEST, true, {E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_UV_LIGHT});
	        } else if (run_time == WATER_CHECK_PRESS_TEST_TIME_POINT) {
	            // 关闭进水阀和 UV 灯
	            set_task_load(E_TASK_ID_WATER_CHECK_PRESS_TEST, false, {E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_UV_LIGHT});
	        }
	    }
	    else if (id == E_TIMER_EVENT_WATER_LED_OFF) {
	        // LED 灯关闭事件
	        if (run_time == LED_OFF_TIME_POINT) {
	            // 关闭 LED 灯
	            protocol::ledControl(false);
	        }
	    }
	    else if (id == E_TIMER_EVENT_WATER_PREVENT_DRY_BURNING) {
	        // 防干烧事件
	        // 当前未实现具体逻辑
	    }
	    else if (id == E_TIMER_EVENT_WATER_HOT_WATER_DELAY) {
	        // 热水延时事件
	        // 当前未实现具体逻辑
	    }
	}

void start_timer_event(ETimerEventId id,int second) {
	if((id == E_TIMER_EVENT_ZERO_STATE_WATER_A || E_TIMER_EVENT_ZERO_STATE_WATER_B == id) && !Global::Water::switch_zero_water)
	{
		LOGD("零陈水关闭");
		return ;
	}
	if(second){
	    if(second > 0) {
			misc::start_timer(id, second, on_timer_cb, NULL);
		}
	}else{
	    if(id < TAB_SIZE(_events_time) && id >= 0) {
			misc::start_timer(id, _events_time[id], on_timer_cb, NULL);
		}
	}
}

void stop_timer_event(ETimerEventId id,bool on) {
//	LOGD("STOP TIMER ID:%d",(int)id);
    misc::stop_timer(id);
    switch(id) {
        case E_TIMER_EVENT_ALL_BRASH:
			set_task_load(E_TASK_ID_ALL_BRASH, false, {
				E_DEV_LOAD_TYPE_UV_LIGHT, E_DEV_LOAD_TYPE_COMMON_TEMP, 
				E_DEV_LOAD_TYPE_COLD_WATER, E_DEV_LOAD_TYPE_COOLING,
				E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WATER_IN, 
				E_DEV_LOAD_TYPE_WASTE_WATER});
			protocol::stopOutputWater();
            break;
        case E_TIMER_EVENT_ICE_ADD_WATER:
			set_task_load(E_TASK_ID_ICE_ADD_WATER, false, {E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_AIR_PUMP,E_DEV_LOAD_TYPE_ICETANK_IN});
            break;

        case E_TIMER_EVENT_MAKE_WATER_TIMEOUT:
			set_task_load(E_TASK_ID_MAKE_WATER_TIMEOUT, false, {E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_AIR_PUMP});
            break;
        case E_TIMER_EVENT_FILTER_BRASH:
			set_task_load(E_TASK_ID_FILTER_BRASH, false, {E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_WASTE_WATER});
            break;
        case E_TIMER_EVENT_DRAIN_WATER_LONG:
			set_task_load(E_TASK_ID_DRAIN_WATER, false, {
				E_DEV_LOAD_TYPE_WASTE_WATER, E_DEV_LOAD_TYPE_COMMON_TEMP,
				E_DEV_LOAD_TYPE_COLD_WATER, E_DEV_LOAD_TYPE_AIR_PUMP,
				E_DEV_LOAD_TYPE_COOLING, E_DEV_LOAD_TYPE_UV_LIGHT,
				E_DEV_LOAD_TYPE_WATER_IN});
			protocol::stopOutputWater();
            break;
        case E_TIMER_EVENT_DRAIN_WATER_SHORT:
			set_task_load(E_TASK_ID_DRAIN_WATER, false, {
				E_DEV_LOAD_TYPE_WASTE_WATER, E_DEV_LOAD_TYPE_COMMON_TEMP,
				E_DEV_LOAD_TYPE_COLD_WATER, E_DEV_LOAD_TYPE_AIR_PUMP,
				E_DEV_LOAD_TYPE_COOLING, E_DEV_LOAD_TYPE_UV_LIGHT,
				E_DEV_LOAD_TYPE_WATER_IN});
			protocol::stopOutputWater();
            break;
        case E_TIMER_EVENT_ZERO_STATE_WATER_A:
        	set_task_load(E_TASK_ID_ZERO_STATE_WATER, false, {E_DEV_LOAD_TYPE_UV_LIGHT, E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WASTE_WATER});
            break;
        case E_TIMER_EVENT_ZERO_STATE_WATER_B:
        	set_task_load(E_TASK_ID_ZERO_STATE_WATER, false, {E_DEV_LOAD_TYPE_UV_LIGHT, E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_AIR_PUMP, E_DEV_LOAD_TYPE_WASTE_WATER});
            break;

        case E_TIMER_EVENT_WATER_CHECK_PRESS_TEST:
			set_task_load(E_TASK_ID_WATER_CHECK_PRESS_TEST, false, {E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_UV_LIGHT});
            break;
        case E_TIMER_EVENT_WATER_LED_OFF:

        	break;
        case E_TIMER_EVENT_WATER_PREVENT_DRY_BURNING:

        	break;
        case E_TIMER_EVENT_WATER_HOT_WATER_DELAY:
			set_task_load(E_TASK_ID_WATER_OPERATE, false, {E_DEV_LOAD_TYPE_WATER_IN, E_DEV_LOAD_TYPE_UV_LIGHT, E_DEV_LOAD_TYPE_AIR_PUMP});
			break;
			//制冰补水
        case E_TIMER_EVENT_ICE_ADD_BLOCK:
        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_MAKEICE_IN, } , false );
			break;
        case E_TIMER_EVENT_MAKE_ICEPUMP:
        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP }, false );
			break;
        case E_TIMER_EVENT_ROD_MOTOR:
        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_PUSH_FORWARD , E_DEV_LOAD_TYPE_PUSH_REVERSE}, false );
			break;
			//停止制冰
        case E_TIMER_EVENT_MAKE_ALLICE:
        	stop_timer_event(E_TIMER_EVENT_ICE_ADD_BLOCK);
        	stop_timer_event(E_TIMER_EVENT_MAKE_ICEPUMP);
        	stop_timer_event(E_TIMER_EVENT_ROD_MOTOR);
        	//发热膜，脱冰阀，推杆电机正负，两风扇，制冰泵，压缩机
        	if(on){
            	set_task_load(E_TASK_ID_MAKE_ICE, false, {E_DEV_LOAD_TYPE_HEAT_FILM,E_DEV_LOAD_TYPE_ICE_OUT,
            			  E_DEV_LOAD_TYPE_COMPRESSOR, });
        	}
			break;
        case E_TIMER_EVENT_ICE_STICK:
            //防沾
        	getProtocolParam().control_aa_a1.CmdIceDispenserMotorForward = false;
        	getProtocolParam().control_aa_a1.CmdIceDispenserMotorReverse = false;
			break;
        case E_TIMER_EVENT_OUT_ICE:
            //出冰
        	getProtocolParam().control_aa_a1.CmdIceDispenserMotorForward = false;
        	getProtocolParam().control_aa_a1.CmdIceDispenserMotorReverse = false;
			break;
        case E_TIMER_EVENT_FORCE_OUT:
            // 关闭制冷 220V 电源，关闭 发热膜 脱冰阀
            set_task_load(E_TASK_ID_MAKE_ICE, false, {E_DEV_LOAD_TYPE_COMPRESSOR, E_DEV_LOAD_TYPE_HEAT_FILM,E_DEV_LOAD_TYPE_ICE_OUT,
            		E_DEV_LOAD_TYPE_PUSH_FORWARD, E_DEV_LOAD_TYPE_PUSH_REVERSE});
			break;
        case E_TIMER_EVENT_INTER_CLEAN:
        	set_task_load(E_TASK_ID_MAKE_ICE, false, { E_DEV_LOAD_TYPE_ICEMAKE_PUMP }, false );
        	getProtocolParam().control_aa_a1.CmdDrainBoxPump = false;
        	break;
        default:
            break;
    }
}

void stop_all_timer_event() {
	for(size_t i = 0; i < TAB_SIZE(_events_time); i++) {
		stop_timer_event((ETimerEventId)i);
	}
}

void reset_timer_event(ETimerEventId id) {
    if(id < TAB_SIZE(_events_time) && id >= 0) 
    misc::reset_timer(id, _events_time[id]);
}

bool is_timer_event_running(ETimerEventId id) {
    return misc::is_timer_running(id);
}

void add_timer_event_ui(timer_cb cb) {
    Mutex::Autolock _l(_s_mutex);
    _s_cb_set.insert(cb);
}

void remove_timer_event_ui(timer_cb cb) {
    Mutex::Autolock _l(_s_mutex);
    _s_cb_set.erase(cb);
}

}
